package com.it.controller;

import com.github.pagehelper.PageInfo;
import com.it.pojo.*;
import com.it.pojo.vo.UserVo;
import com.it.service.*;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author: zgc
 * @version: 1.0
 * @description:
 * @date: 2023/12/15 17:48
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private CompanyService companyService;

    @Autowired
    private UserInfoService userInfoService;

    /*@GetMapping("/list")
    public ModelAndView list(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/pages/user/userList");

        List<UserVo> userList = userService.findAll();
        modelAndView.addObject("userList",userList);
        userList.forEach(System.out::println);

        List<Role> roleList = roleService.findAll();
        modelAndView.addObject("roleList",roleList);

        return modelAndView;
    }*/

    @GetMapping("/list/{pageNo}")
    public ModelAndView listOne(@PathVariable("pageNo") Integer pageNo,@RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/pages/user/userList");

        PageInfo<UserVo> pageInfo = userService.findPage(pageNo, pageSize);
        modelAndView.addObject("pageInfo",pageInfo);

        List<Role> roleList = roleService.findAll();
        modelAndView.addObject("roleList",roleList);

        List<Dept> deptList = deptService.findAll();
        modelAndView.addObject("deptList",deptList);

        List<Company> companyList = companyService.findAll();
        modelAndView.addObject("companyList",companyList);


        return modelAndView;
    }


    @PostMapping("/updateById")
    public String updateById(String roleId,String userId){
        System.out.println("角色"+roleId);
        System.out.println("用户"+userId);
        roleService.updateRoleId(roleId, userId);
        return "redirect:/user/list/1";
    }

    /*删除user表和userinfo表信息*/
    @GetMapping("/deleteById/{userId}/{infoId}")
    public String deleteById(@PathVariable("userId") String userId,@PathVariable("infoId")String infoId){
        userService.updateById(userId,infoId);
        return "redirect:/user/list/1";
    }

    /*修改时回显数据*/
    @GetMapping("/findById/{userId}")
    @ResponseBody
    public UserVo findById(@PathVariable("userId") String userId){
        return userService.findById(userId);
    }
    /*@GetMapping("/findById/{userId}")
    public ModelAndView findById(@PathVariable("userId") String userId){
        ModelAndView modelAndView = new ModelAndView();
        UserVo userVo = userService.findById(userId);
        modelAndView.addObject("userVo",userVo);
        return modelAndView;
    }*/

    /*修改数据*/
    @PostMapping("/update")
    public String update(UserVo userVo){
        userService.updateUserInfo(userVo);
        return "redirect:/user/list/1";
    }

    /*编辑资料时回显数据*/
    @GetMapping("/toEdit/{userId}")
    public ModelAndView findEditById(@PathVariable("userId") String userId){
        System.out.println(userId);
        ModelAndView modelAndView = new ModelAndView();
        UserVo vo = userService.findById(userId);
        System.out.println(vo);
        modelAndView.setViewName("pages/user/editInfo");
        modelAndView.addObject("vo",vo);
        return modelAndView;
    }

    /*编辑用户资料*/
    @PostMapping("/edit")
    public String editInfo(UserVo vo){
        userInfoService.addUserInfo(vo);
        return "/index";
    }




}
